package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.UserLikeApi;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.UserHolder;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public long findloveCount(Long id) {

        Query query = new Query(Criteria.where("userId").is(id));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public long findfanCount(Long id) {
        Query query = new Query(Criteria.where("likeUserId").is(id));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public PageBeanVo findUserListList(Integer page, Integer pagesize, Long id) {

        Query query = new Query(Criteria.where("userId").is(id));
        query.with(new Sort(Sort.Direction.DESC, "created"));
        query.skip((page - 1) * pagesize).limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageBeanVo(page, pagesize, count, userLikeList);
    }

    @Override
    public PageBeanVo findUserList(Integer page, Integer pagesize, Long id) {
        Query query = new Query(Criteria.where("likeUserId").is(id));
        query.with(new Sort(Sort.Direction.DESC, "created"));
        query.skip((page - 1) * pagesize).limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageBeanVo(page, pagesize, count, userLikeList);
    }

    @Override
    public void saveFans(UserLike userLike) {
        mongoTemplate.insert(userLike);
    }

    @Override
    public void deleteUserLike(Long id, Integer uid) {

        Query query = new Query(Criteria.where("userId").is(id).and("likeUserId").is(uid));
        mongoTemplate.remove(query, "user_like");
    }

    @Override
    public boolean isFirstUserLikeLastUser(Long firstUserId, Long lastUserId) {
        final Query query = new Query(
                Criteria.where("userId")
                        .is(firstUserId)
                        .and("likeUserId")
                        .is(lastUserId)
        );
        return mongoTemplate.findOne(query, UserLike.class) != null;
    }

    @Override
    public void addFirstUserLikeLastUser(Long firstUserId, Long lastUserId) {
        final UserLike userLike = new UserLike();
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUserId(firstUserId);
        userLike.setLikeUserId(lastUserId);
        mongoTemplate.insert(userLike);
    }

    @Override
    public void deleteFirstUserLikeLastUser(Long firstUserId, Long lastUserId) {
        final Query query = new Query(
                Criteria.where("userId")
                        .is(firstUserId)
                        .and("likeUserId")
                        .is(lastUserId)
        );
        mongoTemplate.remove(query, UserLike.class);
    }


    @Override
    public List<Long> findUserLikeId(Long id) {

        Query query = new Query(Criteria.where("userId").is(id));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        List<Long> UserLikeId=new ArrayList<>();
        if (userLikeList!=null){
            for (UserLike userLike : userLikeList) {
                UserLikeId.add(userLike.getLikeUserId());
            }
        }

        return UserLikeId;
    }



}
